#!/usr/bin/env bash

# THIS IS A SCRIPT i USE TO CREATE AND UPDATE THE APPLICATION LIST ON https://portable-linux-apps.github.io/
# ONCE CREATED, THE PAGE WILL APPEAR LIKE THIS: https://portable-linux-apps.github.io/apps.html
# FIRST OF ALL, WE NEED TO CREATE A FILE CONTAINING ALL THE ARGUMENTS THAT THIS SCRIPT MUST READ

AMREPO="https://raw.githubusercontent.com/ivan-hc/AM/main"
arch="x86_64"

# ENTER THE DESKTOP DIRECTORY
cd "$(xdg-user-dir DESKTOP)" || exit 1

# GET THE NAMES OF ALL APPLICATION
cp /opt/am/"$arch"-apps "$arch"-apps && echo -e "$(cat ./"$arch"-apps | awk -v FS="(◆ | : )" '{print $2}')" >> args

# GET THE EXACT NUMBER OF AVAILABLE APPS
ARGS=$(cat ./args)
APPS_NUMBER=$(cat ./args | wc -l)

######################################
# COMPILE THE MAIN PAGE OF THE WEBSITE
######################################

wget -q https://raw.githubusercontent.com/Portable-Linux-Apps/Portable-Linux-Apps.github.io/main/index.md -O index.old
sed -n '1,10p' ./index.old > ./index.md
echo "#### *This site lists **$APPS_NUMBER** Appimage packages and standalone applications.*" >> ./index.md
sed -n '12,500p' ./index.old >> ./index.md

###############################
# COMPILE THE APPLICATIONS LIST
###############################

# HEADER
echo "# APPLICATIONS

| [Home](https://portable-linux-apps.github.io/) |
| --- |

#### Here are listed all the apps managed by [AM](https://github.com/ivan-hc/AM) and [AppMan](https://github.com/ivan-hc/AppMan) for the x86_64 architecture.

*Use your browser's built-in search tool to easily navigate to this page.*

#### *This database contains* **$APPS_NUMBER** *Appimage packages and standalone applications*.

-----------------

| ICON | PACKAGE NAME | DESCRIPTION | INSTALLER |
| --- | --- | --- | --- |" > apps.md

# BODY
for arg in $ARGS; do
	description=$(cat "$arch"-apps | grep "◆ $arg :" | sed 's/^.*: //' | sed 's/ *$//')
	echo "| <img src=\"icons/$arg.png\" width=\"48\" height=\"48\"> | [***$arg***](apps/$arg.md) | *$description*..[ *read more* ](apps/$arg.md)*!* | [*blob*](https://github.com/ivan-hc/AM/blob/main/programs/$arch/$arg) **/** [*raw*](https://raw.githubusercontent.com/ivan-hc/AM/main/programs/$arch/$arg) |" >> apps.md
	sleep 0.005
done

# FOOTER
echo "

---

You can improve these pages via a [pull request](https://github.com/Portable-Linux-Apps/Portable-Linux-Apps.github.io/pulls) to this site's [GitHub repository](https://github.com/Portable-Linux-Apps/Portable-Linux-Apps.github.io),  or report any problems related to the installation scripts in the '[issue](https://github.com/ivan-hc/AM/issues)' section of the main database, at [https://github.com/ivan-hc/AM](https://github.com/ivan-hc/AM).

***PORTABLE-LINUX-APPS.github.io is my gift to the Linux community and was made with love for GNU/Linux and the Open Source philosophy.***

---

| [Home](https://portable-linux-apps.github.io/) |
| --- |

--------

# Contacts
- **Ivan-HC** *on* [**GitHub**](https://github.com/ivan-hc)
- **AM-Ivan** *on* [**Reddit**](https://www.reddit.com/u/am-ivan)

###### *You can support me and my work on [**ko-fi.com**](https://ko-fi.com/IvanAlexHC) and [**PayPal.me**](https://paypal.me/IvanAlexHC). Thank you!*

--------

*© 2020-present Ivan Alessandro Sala aka '"'Ivan-HC'"'* - I'm here just for fun!

" >> apps.md

##########################################################
# UPDATE THE JSON FILE FOR FURTER ELECTRON-BASED FRONTENDS
##########################################################

echo "[" > apps.json
for arg in $ARGS; do
	description=$(cat "$arch"-apps | grep "◆ $arg :" | sed 's/"//g' | sed 's/^.*: //')
	echo "  {" >> apps.json
	echo "    \"packageName\": \"$arg\"," >> apps.json
	echo "    \"description\": \"$description..\"," >> apps.json
	echo "    \"icon\": \"https://portable-linux-apps.github.io/icons/$arg.png\"" >> apps.json
	echo "  }," >> apps.json
	sleep 0.005
done
sed -i '$s/,$//' apps.json
echo "]" >> apps.json

######################
# END OF ALL PROCESSES
######################

rm ./args ./index.old ./$arch-apps
